﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseManagement.Models;
using Webdiyer.WebControls.Mvc;

namespace EnterpriseManagement.Controllers
{
    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/

        [AuthorizeGroup(Group = "Super")]
        public ActionResult SearchEmployee()
        {
            List<string> sex = new List<string>();
            sex.Add("M");
            sex.Add("F");
            SelectList sexList = new SelectList(sex);
            ViewData["Sex"] = sexList;

            List<string> position = FormsService.GetAllPositions();
            SelectList positionList = new SelectList(position);
            ViewData["Position"] = positionList;

            List<string> employeeStatus = new List<string>();
            employeeStatus.Add("On");
            employeeStatus.Add("Off");
            SelectList employeeStatusList = new SelectList(employeeStatus);
            ViewData["EmployeeStatus"] = employeeStatusList;

            return PartialView();
        }

        [HttpPost]
        [AuthorizeGroup(Group = "Super")]
        public ActionResult SearchEmployee(SearchEmployeeModel model,string id)
        {
            List<string> sex = new List<string>();
            sex.Add("M");
            sex.Add("F");
            SelectList sexList = new SelectList(sex);
            ViewData["Sex"] = sexList;

            List<string> position = FormsService.GetAllPositions();
            SelectList positionList = new SelectList(position);
            ViewData["Position"] = positionList;

            List<string> employeeStatus = new List<string>();
            employeeStatus.Add("On");
            employeeStatus.Add("Off");
            SelectList employeeStatusList = new SelectList(employeeStatus);
            ViewData["EmployeeStatus"] = employeeStatusList;


            List<Employee> employeelist = FormsService.SearchEmployee(model);
            if (employeelist.Count() == 0)
            {
                ModelState.AddModelError(string.Empty, "未找到相关员工信息");
                return PartialView("ErrorPage");
            }
            else
            {
                List<BasicInformationModel> employeeModelList = new List<BasicInformationModel>();
                foreach (Employee e in employeelist)
                {
                    employeeModelList.Add(new BasicInformationModel(e));
                }
                int currentPage;
                int.TryParse(id,out currentPage);
                if (currentPage <= 0 || currentPage > employeelist.Count() / 8 + (employeelist.Count()%8==0?0:1))
                    currentPage =1;
                PagedList<BasicInformationModel> pageList = new PagedList<BasicInformationModel>(employeeModelList, currentPage, 8);

                return PartialView("SearchEmployeeResult", pageList);
            }

        }

        [AuthorizeGroup(Group="Super")]
        public ActionResult EditEmployee(string id)
        {
            Employee e = FormsService.GetEmployeeById(id);
            if (e == null)
            {
                ModelState.AddModelError(string.Empty, "无此员工信息");
                return PartialView("ErrorPage");
            }
            BasicInformationModel model = new BasicInformationModel(e);
            return PartialView(model);
        }

        [AuthorizeGroup(Group = "Super")]
        [HttpPost]
        public ActionResult EditEmployee(BasicInformationModel model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(string.Empty, "填写信息有误,请确认后再提交");
                return PartialView(model);
            }
            else
            {
                ExecuteResult result=FormsService .UpdateEmployeeById (model );
                if (result == ExecuteResult.Success)
                {
                    ModelState.AddModelError(string.Empty, "更新成功");
                    return PartialView(model);
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "更新失败,请确认你提交的信息是否正确");
                    return PartialView(model);
                }
            }
        }

        
        



    }
}